/** <!--------------------------------------------------------------------------------------------->
 * @file RevGraphBranchLink.h
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */

#ifndef REVGRAPHBRANCHLINK_H
#define REVGRAPHBRANCHLINK_H


// -- includes --
#include <QMap>

// --- forward declarations ---
class RevGraphBranch;

// --- namespaces ---
// --- defines ---
typedef int BranchIndex;


/** <!--------------------------------------------------------------------------------------------->
 * Object which links all graph items together (map iterator).
 *
 * @author	Karl Krach, <gsvn@bluespirit.la>
 * @since	2012-02-11
 <!---------------------------------------------------------------------------------------------> */
class RevGraphBranchLink : public QMap<BranchIndex, RevGraphBranch*>::iterator
{
	/* Friends */
	friend class RevGraphBranchMap;


	/* Con-/Destructors */
public:

	/** <!----------------------------------------------------------------------------------------->
	 * Creates a RevGraphBranchLink object from another RevGraphBranchLink object
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphBranchLink( const RevGraphBranchLink& rOther )
	    : QMap<BranchIndex, RevGraphBranch*>::iterator( rOther ), m_pMap( rOther.m_pMap )
	{
	}


	/* Con-/Destructors */
private:

	/** <!----------------------------------------------------------------------------------------->
	 * Creates a RevGraphBranchLink object
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphBranchLink() : QMap<BranchIndex, RevGraphBranch*>::iterator(), m_pMap(0)
	{
	}

	/** <!----------------------------------------------------------------------------------------->
	 * Creates a RevGraphBranchLink object from an iterator position and a pointer to the map
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphBranchLink( const QMap<BranchIndex, RevGraphBranch*>::iterator& rOther,
	                  const QMap<BranchIndex, RevGraphBranch*>* pMap )
	    : QMap<int, RevGraphBranch*>::iterator( rOther ), m_pMap( pMap )
	{
	}


	/* Methods */
public:

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true, when there is a branch with the path
	 <!-----------------------------------------------------------------------------------------> */
	bool hasBranch( const QString& strPath ) const { return getBranchIndex(strPath) >= 0; }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true, when there is a branch with the given index
	 <!-----------------------------------------------------------------------------------------> */
	bool hasBranchIndex( BranchIndex iBIndex ) { return m_pMap && m_pMap->contains( iBIndex ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true, when there is a successor object in the map
	 <!-----------------------------------------------------------------------------------------> */
	bool hasNext() const { return m_pMap && (*this)+1 != m_pMap->end(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true when there is a predecessor object in the map
	 <!-----------------------------------------------------------------------------------------> */
	bool hasPrevious() const { return m_pMap && (*this) != m_pMap->begin(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the branch index for the given path (or -1 if there is no branch with this path)
	 *
	 * @param strPath (in) path of branch (e.g. /trunk or /wiki)
	 * @returns the branch index or -1 if no branch was found
	 <!-----------------------------------------------------------------------------------------> */
	BranchIndex getBranchIndex( const QString& strPath ) const;

	/** <!----------------------------------------------------------------------------------------->
	 * Returns a list of all branches
	 <!-----------------------------------------------------------------------------------------> */
	QList<RevGraphBranch *> getAll() { return m_pMap->values(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the previous branch
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphBranchLink getPrevious() const { return RevGraphBranchLink( (*this)-1, m_pMap ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns a list of all previous branches
	 <!-----------------------------------------------------------------------------------------> */
	QList<RevGraphBranch *> getAllPrevious() const;

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the next branch
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphBranchLink getNext() const { return RevGraphBranchLink( (*this)+1, m_pMap ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns a list of all following branches
	 <!-----------------------------------------------------------------------------------------> */
	QList<RevGraphBranch *> getAllNext() const;

	/** <!----------------------------------------------------------------------------------------->
	 * Returns a list of all following graphics items. Call hasBranchIndex() before!
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphBranch* getBranch( BranchIndex iBIndex ) const
	{
		return m_pMap->value( iBIndex );
	}


	/* Members */
private:

	/** <!----------------------------------------------------------------------------------------->
	 * Pointer to the map (where the iterator points to). Needed to implement the hasNext() and
	 * hasPrevious() methods.
	 <!-----------------------------------------------------------------------------------------> */
	const QMap<BranchIndex, RevGraphBranch*>* m_pMap;

};	// END class RevGraphBranchLink


#endif // REVGRAPHBRANCHLINK_H
